{% extends "base/base_layout.html" %}
{% load static %}
{% block sidebar_option %}
    sidebar-collapse
{% endblock %}
{% block extra_css %}
    <!-- DataTables -->
    <link rel="stylesheet" href="{% static "adminlte/plugins/datatables-bs4/dataTables.bootstrap4.min.css" %}">
    <link rel="stylesheet" href="{% static "others/css/spinner.css" %}">
    <style>
        #app_icon {
            width: 64px;
            height: 64px;
        }

        textarea {

            width: 100%;
            height: 500px;
            -moz-border-bottom-colors: none;
            -moz-border-left-colors: none;
            -moz-border-right-colors: none;
            -moz-border-top-colors: none;
            background: none repeat scroll 0 0 rgba(0, 0, 0, 0.07);
            border-color: -moz-use-text-color #FFFFFF #FFFFFF -moz-use-text-color;
            border-image: none;
            border-radius: 6px 6px 6px 6px;
            border-style: none solid solid none;
            border-width: medium 1px 1px medium;
            box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12) inset;
            color: #555555;
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            font-size: 1em;
            line-height: 1.4em;
            padding: 5px 8px;
            transition: background-color 0.2s ease 0s;
        }


        textarea:focus {
            background: none repeat scroll 0 0 #FFFFFF;
            outline-width: 0;
        }

        /* disable button */
        {% if not identifier %}
            .disable {
                background-color: #7b7b7b;
                pointer-events: none;
                cursor: default;
            }
        {% endif %}
    </style>
{% endblock %}
{% block content %}
    <div class="content-wrapper">
        <div class="content-header">
        </div>
        <div class="container-fluid">


            <div class="row">
                {% if not identifier %}
                    <div class="col-md-12">
                        <div class="card bg-gradient-danger">
                            <div class="card-header">
                                <h3 class="card-title">Android Runtime 未找到!</h3>
                                <!-- /.card-tools -->
                            </div>
                            <!-- /.card-header -->
                            <div class="card-body">
                                <p> MobSF 找不到 android 实例标识符。 确保 android 实例正在运行并刷新此页面。</p>
                                <p> 如果此错误仍然存在，请设置 <i>ANALYZER_IDENTIFIER</i> in <strong>{{ settings_loc }}</strong> or
                                    通过环境变量 <strong>ANALYZER_IDENTIFIER</strong>.
                                </p>
                            </div>
                            <!-- /.card-body -->
                        </div>
                        <!-- /.card -->
                    </div>
                    <!-- /.col -->
                {% endif %}
                <div class="col-md-12">
                    <div class="card bg-gradient-primary">
                        <!-- /.card-header -->
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-9">
                                    <h4>MobSF 动态扫描支持</h4>
                                    <h5>
                                        <strong>• Genymotion Android VM</strong> version 4.1 - 10.0 (x86, upto API
                                        29)<br/>
                                        <strong>• Android Emulator AVD</strong> (non production) version 5.0 - 9.0 (arm,
                                        arm64, x86, and x86_64 upto API 28)
                                    </h5>
                                    <p>
                                        {% if android_version %}
                                            推荐的安卓版本是 <strong>7.0 +</strong><br/>
                                            检测到安卓版本: <strong>{{ android_version }}</strong>, SDK:
                                            <strong>{{ android_sdk }}</strong> <br/>
                                        {% endif %}

                                        {% if android_version < 5 %}
                                            Xposed 框架用于检测，您必须 MobSFy 并在动态分析之前重新启动 Android 运行时。
                                            {% if android_version < 4.4 %}
                                                <br/>For Android version <strong>4.1 - 4.3</strong>, set Android VM
                                                proxy as<strong>{% if proxy_ip %} {{ proxy_ip }}{% else %}
                                                <i>&lt;Host IP&gt;</i>{% endif %}:{{ proxy_port }}</strong>
                                            {% endif %}
                                        {% elif android_version %}
                                            Frida will be used for instrumentation.
                                        {% endif %}

                                    </p>
                                </div>
                                <div class="col-md-3">
                                    <br/>
                                    <br/>
                                    <br/>
                                    <p>
                                        <button class="btn btn-info disable" role="button" data-toggle="modal"
                                                data-target="#mmobsfy">MobSFy Android Runtime
                                        </button>
                                    <p>安卓实例: <strong>{{ identifier }}</strong></p></div>
                                </p>
                            </div>
                        </div>
                    </div>
                    <!-- /.card-body -->
                </div>
                <!-- /.card -->
            </div>
            <!-- /.col -->

            <div class="col-md-12">
                <div class="card card-default">
                    <div class="card-header">
                        <h3 class="card-title"><i class="fa fa-rocket"></i> MobSF 动态扫描</h3>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <h4> 可用的应用程序</h4>
                            <table class="table table-bordered table-hover table-striped">
                                <thead>
                                <tr>
                                    <th>APP</th>
                                    <th>文件名称</th>
                                    <th>包</th>
                                    <th>ACTION</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% if apps %}
                                    {% for e in apps %}
                                        <tr>
                                            <td align="center">
                                                <img id="app_icon"
                                                     src="{% if e.ICON_FOUND %}/download/{{ e.MD5 }}-icon.png{% else %}{% static 'img/no_icon.png' %}{% endif %}"/>
                                                <br/><strong>{{ e.APP_NAME }} - {{ e.VERSION_NAME }}</strong>
                                            </td>
                                            <td>
                                                {{ e.FILE_NAME }}
                                            </td>
                                            <td>
                                                {{ e.PACKAGE_NAME }}
                                            </td>
                                            <td align="center">
                                                <p>
                                                    <a class="btn btn-success disable" onclick="dynamic_loader()"
                                                       href="../android_dynamic/{{ e.MD5 }}"><i
                                                            class="fab fa-android"></i> 开始动态分析</a>
                                                </p>
                                                <p>
                                                    <a class="btn btn-primary btn-sm disable" onclick="dynamic_loader()"
                                                       href="../android_dynamic/{{ e.MD5 }}?re_install=0"><i
                                                            class="fab fa-android"></i> 开始动态分析 (without Re-install)</a>
                                                </p>
                                                <p>
                                                    <a class="btn btn-info" href="../dynamic_report/{{ e.MD5 }}"><i
                                                            class="fa fa-mobile"></i> 查看报告 </a>
                                                </p>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                {% endif %}
                                </tbody>
                            </table>
                            </br>
                            <h4>app的设备</h4>
                            <table class="table table-bordered table-hover table-striped">
                                <thead>
                                <tr>
                                    <th>APP</th>
                                    <th>LOCATION IN DEVICE</th>
                                    <th>ACTION</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% if device_packages %}
                                    {% for hash, det in device_packages.items %}
                                        <tr>
                                            <td align="center">
                                                <img id="app_icon" src="/download/{{ hash }}-icon.png"
                                                     onerror="this.onerror=null;this.src='{% static 'img/no_icon.png' %}'"/>
                                                <br/><strong>{{ det.0 }}</strong>
                                            </td>
                                            <td>
                                                {{ det.1 }}
                                            </td>
                                            <td align="center">
                                                <p>
                                                    <a class="btn btn-primary disable" onclick="dynamic_loader()"
                                                       href="../android_dynamic/{{ hash }}?re_install=0&install=0"><i
                                                            class="fab fa-android"></i> 开始动态扫描</a>
                                                </p>
                                                <p>
                                                    <a class="btn btn-warning disable" onclick="dynamic_loader()"
                                                       href="../static_scan/{{ hash }}"><i class="fa fa-download"></i>
                                                        Pull &amp; Static Analysis</a>
                                                </p>
                                                <p>
                                                    <a class="btn btn-info" href="../dynamic_report/{{ hash }}"><i
                                                            class="fa fa-mobile"></i> 查看报告</a>
                                                </p>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                {% endif %}
                                </tbody>
                            </table>

                        </div>
                    </div>
                    <!-- /.card-body -->
                </div>
                <!-- /.card -->
            </div>
            <!-- /.col -->
        </div>

    </div>
    </div>
    </div>
    </div>
    </div>

    <!--Modal-->


    <div class="modal" id="mmobsfy" tabindex="-1" role="dialog">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">MobSFy Android Runtime</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form role="form">
                        <div class="form-group">
                            <label>IP &amp; Port/Identifier</label>
                            <input id="identifier" type="text" class="form-control" value="{{ identifier }}">
                        </div>
                        <div class="form-group">
                            <label>Status</label>
                            <textarea id="stat" class="form-control" rows="10" placeholder="" disabled=""></textarea>
                        </div>

                    </form>
                </div>
                <div class="modal-footer">
                    <button id="mobsfy" type="button" class="btn btn-primary disable">MobSFy!</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>

    <!--Loader-->
    <div class="hidden loading">
        <div class='uil-ring-css' style='transform:scale(0.79);'>
            <div></div>
        </div>
    </div>
{% endblock %}
{% block extra_scripts %}
    <!-- DataTables -->
    <script src="{% static "adminlte/plugins/datatables/jquery.dataTables.min.js" %} "></script>
    <script src="{% static "adminlte/plugins/datatables-bs4/dataTables.bootstrap4.min.js" %}"></script>
    <script>
        function dynamic_loader() {
            var loadingOverlay = document.querySelector('.loading');
            loadingOverlay.classList.remove('hidden');
        }

        //MobSFy AJAX
        $("#mobsfy").one("click", function () {
            $('#stat').append("\nMobSFying Android Runtime\n");
            $.ajax({
                url: "../mobsfy/",
                type: "POST",
                dataType: "json",
                data: {
                    csrfmiddlewaretoken: '{{ csrf_token }}',
                    identifier: $('#identifier').val(),
                },
                success: function (json) {
                    if (json.status === "ok") {
                        $('#stat').append("\nSucessfully created MobSF Dynamic Analysis environment.");
                        if (json.android_version < 5) {
                            $('#stat').append("\n* Download & Install Xposed Framework.\n* Restart the device.");
                            $('#stat').append("\n* Enable all the Xposed modules.\n* Restart the device once more.");
                        } else {
                            $('#stat').append("\nMobSF agents and Frida server installed.");
                        }

                    } else if (json.status === "failed") {
                        $('#stat').append(json.message);
                    }
                    $('#stat').scrollTop($('#stat')[0].scrollHeight);
                },
                error: function (xhr, errmsg, err) {
                    $('#stat').append("\nERROR!!! \n\n" + err);
                    $('#stat').scrollTop($('#stat')[0].scrollHeight);
                }
            });
            return false;
        });


        //Datatables
        $(function () {
            // Datatable
            $('table').DataTable({
                "paging": true,
                "lengthChange": false,
                "searching": true,
                "ordering": false,
                "info": true,
                "autoWidth": true,
                "responsive": true,
            });
        });

    </script>
{% endblock %}
